/*
 *   A task scheduler that implements Monte-Carlo simulation
 *
 *   Copyright (C) 2011 Systasis Computer Systems, Inc.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   $Id: GanttPresenter.java 82 2011-08-07 17:35:42Z jchimene@gmail.com $
 */
package com.systasis.sked.client.presenter;

import java.util.Date;
import java.util.logging.Logger;

import com.arcetis.gwt.jsgantt.client.TaskItem;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.PopupView;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.systasis.sked.client.model.GanttTask;
import com.systasis.sked.client.view.GanttHandlers;

@Singleton
public class GanttPresenter extends Presenter<GanttPresenter.MyView, GanttPresenter.MyProxy> implements GanttHandlers.GanttUiHandlers {

    @ProxyCodeSplit
    @NameToken("ganttChart")
    public interface MyProxy extends ProxyPlace<GanttPresenter> {
    }

    public interface MyView extends PopupView, HasUiHandlers<GanttHandlers.GanttUiHandlers> {
	void add(TaskItem ganttTask);

	void generateSampleContent();
    }

    @SuppressWarnings("unused")
    final private Logger logger = Logger.getLogger(getClass().getName());
    final private ActivityOnNodeScheduler scheduler;

    @Inject
    public GanttPresenter(final EventBus eventBus //
	    , final MyView view //
	    , final MyProxy proxy //
	    , final ActivityOnNodeScheduler scheduler) {
	super(eventBus, view, proxy);
	this.scheduler = scheduler;
    }

    @SuppressWarnings({ "deprecation", "unused" })
    public void schedulerToGantt() {
	final DateTimeFormat dtFormat = DateTimeFormat.getFormat("MM/dd/yy");

	if (false) {
	    getView().generateSampleContent();
	    return;
	}

	Date currentStart = getStartDate();
	for (final Activity activity : scheduler.activityIterable()) {
	    final TaskItem ganttTask = new GanttTask.GanttTaskBuilder() //
		    .id(activity.getId()) //
		    .name(activity.getActivityName()) //
		    .startDate(dtFormat.format(currentStart)) //
		    .endDate(dtFormat.format(getEndDate(currentStart, activity.getDurationLikely()))) //
		    .depends(getPredecessor(activity)) //
		    .build();
	    currentStart = ganttTask.getEndDate();
	    getView().add(ganttTask);
	}
    }

    @SuppressWarnings("deprecation")
    private Date getEndDate(Date startDate, double durationLikely) {
	startDate.setHours((int) (startDate.getHours() + (24 * durationLikely)));
	return startDate;
    }

    private String getPredecessor(Activity activity) {
	if (0 == activity.getPredecessorList().size()) {
	    return null;
	}

	final StringBuffer r1 = new StringBuffer();
	for (final Integer r0 : activity.getPredecessorList()) {
	    r1.append(Integer.valueOf(r0)).append(",");
	}
	return r1.substring(0, r1.length() - 1).toString();
    }

    private Date getStartDate() {
	return new Date();
    }

    @SuppressWarnings("unused")
    private int getUnixTimeStamp() {
	final Date date = new Date();
	final int iTimeStamp = (int) (date.getTime() * .001);
	return iTimeStamp;
    }

    @Override
    protected void revealInParent() {
	getView().show();
    }
}